<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Grom Hellscream
 * Date: 11/16/12
 * Time: 3:57 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
jimport('joomla.html.pagination');

/**
 * Canbotheodonvi Model
 */
class VastModelCanbotheodonvi extends JModelItem
{
    private $_total = null;
    private $_pagination = null;
    protected $canbos;

    public function __construct()
    {
        parent::__construct();
        $config = JVastConfig::getConfig();
        
        $this->setState('limit', (isset($config['numOfItemsPerPage']) ? (int)$config['numOfItemsPerPage'] : JRequest::getVar('limit', 5, '', 'int')));
        $this->setState('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));
    }

    /**
     * Get the message
     * @return string The message to be displayed to the user
     */
    public function getCanbos()
    {
        $id = JRequest::getInt('id');

        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__hosocanbo AS a');
        $query->leftJoin('#__congtac AS ct ON ct.hosocanbo_id = a.id');
        $query->where('ct.donvi_id = ' . $id);

        $db->setQuery($query);
        $this->canbos = $db->loadObjectList();

        $this->_total = count($this->canbos);
        return $this->canbos;
    }

    public function getPagination()
    {
        $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
        return $this->_pagination;
    }

    public function getTotal()
    {
        return $this->_total;
    }
    
    public function getKhoi() 
    {
        $id = JRequest::getInt('id');
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        
        $query->select('a.*');
        $query->from('#__donvi as a');
        $query->where('a.id = ' . $id);
        
        $db->setQuery($query);
        $donvi = $db->loadObject();
        
        if ($donvi !== NULL)
        {
            $khoiId = $donvi->khoi_id;
            $query = $db->getQuery(true);
            
            $query->select('a.*');
            $query->from('#__khoi as a');
            $query->where('a.id = ' . $khoiId);
            
            $db->setQuery($query);

            return $db->loadObject();                
        }
        
        return NULL;
    }   
    
    public function getDonvi()
    {
        $id = JRequest::getInt('id');

        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__donvi AS a');
        $query->where('a.id = ' . $id);

        $db->setQuery($query);
        $donvi = $db->loadObject();
        return $donvi;
    }     
}